﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace teacherplanner
{
    public partial class CreateDbForm : Form
    {
        public CreateDbForm()
        {
            InitializeComponent();
        }

        private void createButton_Click(object sender, EventArgs e)
        {
            if (this.StaffInits.Text == "" | this.yearComboBox.Text == "")
            {
                MessageBox.Show("Please complete all boxes", "Insufficient Information", MessageBoxButtons.OK);

                return;

            }

            string myFileName = "Blundells_Staff_Planner_" + StaffInits.Text + "_" + yearComboBox.Text;
            string newFilePath = "C:\\DATA\\" + myFileName + ".mdb";
            teacherplanner.Properties.Settings.Default.CurrentPlanner = newFilePath;

            if (File.Exists(newFilePath)) 
            {
                DialogResult myResult = MessageBox.Show("Database file already exists." + "\r\n" + "Are you sure"
                    +" that you want to overwrite?","Warning!",MessageBoxButtons.YesNo);
                if (myResult == DialogResult.No)
                {
                    return;
                }
            }

            Stream objStream = null;

            FileStream objFileStream = null;

            try
            {

                byte[] abytResource;
                System.Reflection.Assembly objAssembly = System.Reflection.Assembly.GetExecutingAssembly();

                string[] myResources = objAssembly.GetManifestResourceNames();
                objStream = objAssembly.GetManifestResourceStream("teacherplanner.blankdb.mdb");
                abytResource = new Byte[objStream.Length];
                objStream.Read(abytResource, 0, (int)objStream.Length);
                objFileStream = new FileStream("C:\\DATA\\" + myFileName + ".mdb", FileMode.Create);
                objFileStream.Write(abytResource, 0, (int)objStream.Length);
                objFileStream.Close();

            }

            catch (Exception)
            {
                throw new NullReferenceException();
            }

            finally
            {
                if (objFileStream != null)
                {
                    objFileStream.Close();
                    objFileStream = null;
                }

                if (objStream != null)
                {
                    objStream = null;
                }
            }
            DataBase.Edit.CreateTable("CREATE TABLE Admin (ID INT IDENTITY(1,1) NOT NULL, ParamName VARCHAR(25), ParamVal VARCHAR(25))");
            String[] InsertArray = {@"INSERT INTO Admin VALUES (1,'Initials','" + StaffInits.Text + "')",@"INSERT INTO Admin VALUES (2,'Year','" + yearComboBox.Text + "')"};
            DataBase.Edit.ApplySQLArray(InsertArray);

            teacherplanner.CreateDbForm.ActiveForm.Hide();
            teacherplanner.DefineClasses myForm = new teacherplanner.DefineClasses();
            myForm.Show();
        }
    }

}
